<?php 
require_once("inc_security.php");
//check quyen them sua xoa
checkAddEdit("edit");
    $errorMsg = "";
	//Khai bao Bien
	$fs_redirect 							= base64_decode(getValue("url","str","GET",base64_encode("listing.php")));
	$record_id 								= getValue("record_id");
//Call Class generate_form();
	$myform 									= new generate_form();
	//Loai bo chuc nang thay the Tag Html
	
    $new_cat_id = new db_query("SELECT new_cat_id 
                                                FROM news
                                                WHERE new_id =".$record_id);
    while ($row = mysql_fetch_assoc($new_cat_id->result))
    {
        $new_cat_id_current = $row['new_cat_id'];
    }
    
    $categoriesArr = array();
    $categories = new db_query("SELECT * 
										 FROM categories_news
										 ORDER BY cat_id DESC
										 ");
    while($row	=	mysql_fetch_assoc($categories->result)){
        $categoriesArr[$row['cat_id']] = $row['cat_name'];
    }
	$myform->add("new_title","new_title",0,0,"",0,translate_text("Vui lòng nhập tiêu đề cho tin tức"),0,"");
	$myform->add("new_cat_id","new_cat_id",1,0,'',0,"",0,"");
    $myform->add("new_date", "new_date", 1, 0,"",0, "", 0, "", 0, "");
    $myform->add("new_content_summary","new_content_summary",6,0,"",1, "Vui lòng điền nội dung tóm tắt của tin tức");
	//Add table
	$myform->addTable($fs_table);
	//Warning Error!
	//Get Action.
	$action	= getValue("action", "str", "POST", "");
	if($action == "execute"){
	   $upload  = new upload("new_image", $path, $alow_extention,300);
       $new_image	=	$upload->file_name;
	   If($new_image  != ""){
			$myform->add("new_image", "new_image", 0, 1, "Vui lòng thêm ảnh đại diện cho tin tức", 0, "", 0, ""); // 0 là lấy từ control, 1 lấy từ biến
		}
		
		
		$errorMsg .= $myform->checkdata();
        $myform->add("new_content","new_content", 0, 0, 0, 0,"", 0,"");
		echo $errorMsg;
		if($errorMsg == ""){
		  $myform->removeHTML(0);
			$db_ex 	= new db_execute_return();
			$last_id = $db_ex->db_execute($myform->generate_update_SQL($id_field, $record_id));
			
			redirect("listing.php");
		
		}
	}
	//add form for javacheck
	$myform->addFormname("add_new");
	$myform->evaluate();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php echo $load_header?>
<?php  
$myform->checkjavascript();
$errorMsg .= $myform->strErrorField;

//lay du lieu cua record can sua doi
$db_data 	= new db_query("SELECT * FROM " . $fs_table . " WHERE " . $id_field . " = " . $record_id);
if($row 		= mysql_fetch_assoc($db_data->result)){
	foreach($row as $key=>$value){
		if($key!='lang_id' && $key!='admin_id') $$key = $value;
	}
}else{
		exit();
}
?>
</head>
<body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<?php  /*------------------------------------------------------------------------------------------------*/ ?>
<?php echo template_top(translate_text("Edit_city"))?>
<?php  /*------------------------------------------------------------------------------------------------*/ ?>
	<p align="center" style="padding-left:10px;">
	<?php 
	$form = new form();
	$form->create_form("add", $_SERVER["REQUEST_URI"], "post", "multipart/form-data",'onsubmit="validateForm(); return false;"');
	$form->create_table();
	?>
	
	<?php echo $form->text("Tiêu đề", "new_title", "new_title", $new_title, "Tiêu đề", 1, 240, "", 255, "", "", "")?>
	<?php echo $form->select("Chuyên mục", "new_cat_id", "new_cat_id", $categoriesArr,$new_cat_id_current, "Chuyên mục tin tức")?>
    <?php echo $form->getFile("Chọn ảnh","new_image","new_image","ảnh đại diện",1,34);?>
    <?php echo $form->button("hidden","new_date","new_date",time(),"");?>
    
    <?php echo $form->close_table()?>
    <?php echo $form->wysiwyg("Nội dung tóm tắt", "new_content_summary", $new_content_summary, "../../resource/wysiwyg_editor/","80%",450)?>
	<?php echo $form->wysiwyg("Nội dung", "new_content", $new_content, "../../resource/wysiwyg_editor/","80%",450)?>	
    <?php echo $form->create_table("2")?>
	<?php echo $form->button("submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "Cập nhật" . $form->ec . "Làm lại", "Cập nhật" . $form->ec . "Làm lại", '' . $form->ec . '', "");?>
	<?php echo $form->hidden("action", "action", "execute", "");?>
	
	<?php 
	$form->close_table();
	$form->close_form();
	unset($form);
	?>
	</p>
<?php echo template_bottom() ?>
<?php  /*------------------------------------------------------------------------------------------------*/ ?>
</body>
</html>